package org.swim.utils;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.WordUtils;

@SuppressWarnings("rawtypes")
public class SwimLog {

	private static SwimLog sl;

	private Type type = Type.CUT;

	private boolean debug = false;
	private boolean log = false;

	private Set<String> debugClasses;

	public enum Type {
		ELOQUENT, SYNETIC, CUT, MUTE
	}

	private SwimLog() {
	}

	public static SwimLog getSwimLog() {
		if (sl == null) {
			sl = new SwimLog();
		}

		return sl;
	}

	public static SwimLog getSwimLog(Class... classes) {
		if (sl == null) {
			sl = new SwimLog();
		}

		sl.addDebugClasses(classes);

		return sl;
	}

	public void debug(Class source, String message) {
		if (!debug)// || debugClasses == null ||
					// !debugClasses.contains(source.getCanonicalName()))
			return;

		log(source, message);
	}

	public void log(Class source, String message) {
		if (!log)
			return;

		switch (type) {
		case CUT:
			System.out.println("[SwimLog - class: " + source.getSimpleName()
					+ "] " + WordUtils.wrap(message, 256));
			break;

		case SYNETIC:
			System.out.println("[SwimLog - class: " + source.getSimpleName()
					+ "] " + message);
			break;
		case ELOQUENT:
			System.out.println("[SwimLog - millis: "
					+ System.currentTimeMillis() + " - class: "
					+ source.getCanonicalName() + "] " + message);
			break;

		}
	}

	private void addDebugClasses(Class... source) {
		if (debugClasses == null)
			debugClasses = new HashSet<String>();

		for (Class class1 : source) {
			debugClasses.add(class1.getCanonicalName());
		}
	}

	public void clearDebugClasses() {
		debugClasses = null;
	}

}
